<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <meta name="author" content="Rogerspy">
  <link rel="canonical" href="https://pytorch-zh.gitee.io/torchnn/modulelist/">
  <link rel="shortcut icon" href="/pytorch-zh/img/favicon.ico">
  <title>Modulelist - Pytorch 中文文档（1.4.0）</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700" />

  <link rel="stylesheet" href="../../css/theme.css" />
  <link rel="stylesheet" href="../../css/theme_extra.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" />
  
  <script>
    // Current page data
    var mkdocs_page_name = "Modulelist";
    var mkdocs_page_input_path = "torchnn\\modulelist.md";
    var mkdocs_page_url = "/torchnn/modulelist/";
  </script>
  
  <script src="../../js/jquery-2.1.1.min.js" defer></script>
  <script src="../../js/modernizr-2.8.3.min.js" defer></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
    <div class="wy-side-scroll">
      <div class="wy-side-nav-search">
	    <img src='/pytorch-zh/img/logo.svg'/>
        <a href="../.." class="icon icon-home"> Pytorch 中文文档（1.4.0）</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="../..">主页</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="../../get-started/">60分钟快速入门</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="../../faq/">PyTorch FAQ</a>
                    </li>
                </ul>
                <p class="caption"><span class="caption-text">两种基本结构</span></p>
                <ul class="current">
                    <li class="toctree-l1"><a class="reference internal" href="../parameters/">参数 Parameters</a>
                    </li>
                    <li class="toctree-l1 current"><a class="reference internal current" href="#">容器 Containers</a>
    <ul class="current">
                <li class="toctree-l2"><a class="reference internal" href="../module/">Module</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../sequential/">Sequential</a>
                </li>
                <li class="toctree-l2 current"><a class="reference internal current" href="./">Modulelist</a>
    <ul class="current">
    <li class="toctree-l3"><a class="reference internal" href="#append">append</a>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#extend">extend</a>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#insert">insert</a>
    </li>
    </ul>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../moduledict/">Moduledict</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../parameterlist/">Parameterlist</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../parameterdict/">Parameterdict</a>
                </li>
    </ul>
                    </li>
                </ul>
                <p class="caption"><span class="caption-text">网络层</span></p>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="#">卷积层</a>
    <ul>
                <li class="toctree-l2"><a class="reference internal" href="../conv1d/">Conv1d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../conv2d/">Conv2d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../conv3d/">Conv3d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../convtranspose1d/">Convtranspose1d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../convtranspose2d/">Convtranspose2d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../convtranspose3d/">Convtranspose3d</a>
                </li>
    </ul>
                    </li>
                </ul>
      </div>
    </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../..">Pytorch 中文文档（1.4.0）</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../..">Docs</a> &raquo;</li>
    
      
        
          <li>容器 Containers &raquo;</li>
        
      
        
          <li>两种基本结构 &raquo;</li>
        
      
    
    <li>Modulelist</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <h2 id="modulelist">ModuleList<a class="headerlink" href="#modulelist" title="Permanent link">&para;</a></h2>
<div class='important'>
    <pre>
    <font color='red'>CLASS</font> torch.nn.ModuleList(
        <i>modules: Optional[
            Iterable[torch.nn.modules.module.Module]
        ] = None</i>
    )
    </pre>
</div>

<p>将所有子模块包装在一个列表中。</p>
<p><code>ModuleList</code> 可以像常规的 Python 列表那样进行索引，但是其中包含的模块已经正确注册，并且对所有的 <code>Module</code> 方法都可见。</p>
<blockquote>
<p><strong><em>参数</em></strong></p>
<ul>
<li><em>modules (iterable, optional)</em> ：可迭代的模块。</li>
</ul>
</blockquote>
<p>例子：</p>
<div class="codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">linears</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">ModuleList</span><span class="p">([</span><span class="n">nn</span><span class="o">.</span><span class="n">Linear</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)])</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="c1"># ModuleList 是可迭代的，或者可以用整数进行索引</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">l</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">linears</span><span class="p">):</span>
            <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">linears</span><span class="p">[</span><span class="mi">1</span> <span class="o">//</span> <span class="mi">2</span><span class="p">](</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">l</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">x</span>
</code></pre></div>

<h3 id="append">append<a class="headerlink" href="#append" title="Permanent link">&para;</a></h3>
<blockquote>
<div class="codehilite"><pre><span></span><code><span class="n">append</span><span class="p">(</span><span class="n">module</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">modules</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">Module</span><span class="p">)</span> <span class="err">→</span> <span class="n">T</span>
</code></pre></div>

<p>向列表末尾添加模块</p>
<p><strong><em>参数</em></strong></p>
<ul>
<li><em>module (nn.Module)</em>：需要添加的模块</li>
</ul>
</blockquote>
<h3 id="extend">extend<a class="headerlink" href="#extend" title="Permanent link">&para;</a></h3>
<blockquote>
<div class="codehilite"><pre><span></span><code><span class="n">extend</span><span class="p">(</span><span class="n">modules</span><span class="p">:</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">modules</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">Module</span><span class="p">])</span> <span class="err">→</span> <span class="n">T</span>
</code></pre></div>

<p>向列表末尾添加可迭代的 Python 模块</p>
<p><strong><em>参数</em></strong></p>
<ul>
<li><em>modules (iterable)</em>：需要添加的可迭代模块</li>
</ul>
</blockquote>
<h3 id="insert">insert<a class="headerlink" href="#insert" title="Permanent link">&para;</a></h3>
<blockquote>
<div class="codehilite"><pre><span></span><code><span class="n">insert</span><span class="p">(</span><span class="n">index</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">module</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">modules</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">Module</span><span class="p">)</span> <span class="err">→</span> <span class="kc">None</span>
</code></pre></div>

<p>将模块插入到列表给定索引之前</p>
<p><strong><em>参数</em></strong></p>
<ul>
<li><em>index (int)</em>：需要插入的索引位置</li>
<li><em>module (nn.Module)</em>：需要插入的模块</li>
</ul>
</blockquote>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../moduledict/" class="btn btn-neutral float-right" title="Moduledict">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="../sequential/" class="btn btn-neutral" title="Sequential"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
      <p>©2020 Rogerspy. All rights reserved.</p>
    
  </div>

  Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
        <span><a href="../sequential/" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="../moduledict/" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '../..';</script>
    <script src="../../js/theme.js" defer></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML" defer></script>
      <script src="../../search/main.js" defer></script>
    <script defer>
        window.onload = function () {
            SphinxRtdTheme.Navigation.enable(true);
        };
    </script>

</body>
</html>
